package com.sunfeng.netty.heima.day02.promise;

import io.netty.channel.DefaultEventLoop;
import io.netty.util.concurrent.DefaultPromise;
import lombok.extern.slf4j.Slf4j;

/**
 * @ClassName Demo04
 * @Description TODO   同步处理任务失败await
 * @Author 孙峰
 * @Date 2022/10/25 14:29
 */
@Slf4j
public class Demo04 {
    public static void main(String[] args) throws InterruptedException {
        DefaultEventLoop eventExecutors = new DefaultEventLoop();
        DefaultPromise<Integer> promise = new DefaultPromise<>(eventExecutors);

        eventExecutors.execute(() -> {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            RuntimeException e = new RuntimeException("error...");
            log.debug("set failure, {}", e.toString());
            promise.setFailure(e);
        });
        log.debug("start...");
        log.debug("{}", promise.getNow());
        promise.await();
        log.debug("result:{}",promise.isSuccess()?promise.getNow():promise.cause());
        eventExecutors.shutdownGracefully();
    }
}
